Professor.prototype.tSkill = 'JAVA';
function Professor() { }
var professor = new Professor();
Teacher.prototype = professor;
function Teacher() {
    this.mSkill = 'JS/JQ';
    this.success = {
        alibaba: '28',
        Itencent: '30'
    };
    this.students = 500;
}
var teacher = new Teacher();
Student.prototype = teacher;
function Student() {
    this.pSkill = 'HTML/CSS';
}
var student = new Student();
// student.success.baidu = '100';
console.log(teacher, student);
student.students++//自身变为501,teacher的值不变,相当于自己给自己创建一个新属性
console.log(teacher, student);

//Object.create(对象，可为null）创建对象
var test = { num: 2 };
function Obj() { };
Obj.prototype.num = 1;
//var obj1 = Object.create(Obj.prototype);
var obj1 = Object.create(test);
var obj2 = new Obj();
//实例化obj2
// //调用构造函数Obj的初始化属性和方法
// //指定实例对象的原型
console.log(obj1);
console.log(obj2);


function Car() {
    this.brand = 'Benz';
}
Car.prototype = {
    brand: 'Mazda',
    intro: function () {
        console.log('我是' + this.brand + '车');
    }
}
var car = new Car();
car.intro();


// 方法重写
function Compute() {
    this.plus = function (a, b) {
        console.log(a + b);
    },
        this.minus = function (a, b) {
            console.log(a - b);
        }
}
function FullCompute() {
    Compute.apply(this);// 改变this指向
    this.mul = function (a, b) {
        console.log(a * b);
    }
    this.div = function (a, b) {
        console.log(a / b);
    }
}
var compute = new FullCompute();
compute.plus(1, 2);
compute.minus(1, 2);
compute.mul(1, 2);
compute.div(1, 2);

// 原型继承技巧
function Teacher() {
    this.name = 'Mr. Li';
    this.tSkill = 'JAVA';
    this.success = {
        alibaba: '28',
        Itencent: '30'
    };
    this.students = 500;
}
Teacher.prototype = {
    pSkill: "JS/JQ"
}
var t = new Teacher();
console.log(t);
function Student() {
    this.name = 'Mr. Wang';
}
function Buffer() { }
Buffer.prototype = Teacher.prototype;
var buffer = new Buffer();
Student.prototype = buffer;
Student.prototype.age = 18;
var s = new Student()
console.log(s, t);


//继承插件
function inherit(Target, Origin) {
    function Buffer() { }
    Buffer.prototype = Origin.prototype;
    Target.prototype = new Buffer();
    Target.prototype.constructor = Target;
    Target.prototype.uber = Origin;
}

function Teacher() { }
function Student() { }
function Buffer() { }
inherit(Student, Teacher);
var stu = new Student();
var tea = new Teacher();
console.log(stu);
console.log(tea);

// 立即执行函数写法
var inherit = (function () {
    var Buffer = function () { }
    // 用Buffer对象缓冲,防止子原型属性污染父级原型,实现继承
    return function (Target, Origin) {
        Buffer.prototype = Origin.prototype;
        Target.prototype = new Buffer();
        Target.prototype.constructor = Target;
        Target.prototype.super_class = Origin;
    }
})();
Teacher.prototype.name = 'Mr. Zhang';
function Teacher() { }
function Student() { }
// function Buffer() { }
inherit(Student, Teacher);
Student.prototype.age = 18;
var s = new Student();
var t = new Teacher();
console.log(s);
console.log(t);


//初始化案例:程序员
var initProgrammer = (function () {
    var Programmer = function () { }
    Programmer.prototype = {
        name: '程序员',
        tool: '计算机',
        work: '编写应用程序',
        duration: '10小时',
        say: function () {
            console.log("我是一名" + this.myName + this.name + '，我的工作是用' + this.tool + this.work + '，我每天工作' + this.duration + '   ，我的工作需要用到' + this.lang.toString() + "。");
        }
    }
    function FrontEnd() { };
    function BackEnd() { };
    inherit(FrontEnd, Programmer);
    inherit(BackEnd, Programmer);
    FrontEnd.prototype.lang = ['Html', 'CSS', 'JavaScript'];
    FrontEnd.prototype.myName = "前端";
    BackEnd.prototype.lang = ['Java', 'C', 'Python'];
    BackEnd.prototype.myName = "后端";
    return {
        BackEnd: BackEnd,
        FrontEnd: FrontEnd
    }
})();

var frontEnd = new initProgrammer.FrontEnd();
var backEnd = new initProgrammer.BackEnd();
frontEnd.say();
backEnd.say();